package com.dropgame.tools;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.dropgame.main.AssetLoader;
import com.dropgame.main.DropGame;

public class TransitionEffect {

	/*
	 * This class is used to create a fade in/out effect between screens.
	 */
	static final float FADE_DURATION = 0.5f;
	static final int WORLD_WIDTH = Gdx.graphics.getWidth();
	static final int WORLD_HEIGHT = Gdx.graphics.getHeight();
	public boolean enteringScreen = true;
	public boolean leavingScreen = false;
	OrthographicCamera guiCam;
	
	public DropGame game;
	public Screen nextScreen;
	
	float fadeTime = 0;
	float fadeAlpha = 0;
	
	public TransitionEffect(DropGame game){
		this.game = game;
		nextScreen = null;
		guiCam = new OrthographicCamera(WORLD_WIDTH, WORLD_HEIGHT);
	    guiCam.position.set(WORLD_WIDTH / 2, WORLD_HEIGHT / 2, 0);
	}
	
	public void nextScreen(Screen screen)
	   {
	      fadeTime = 0;
	      leavingScreen = true;
	      nextScreen = screen;
	   }
	
	public void movingScreen(SpriteBatch batcher, float deltaTime){
	
	if (enteringScreen) {
        fadeTime += deltaTime;
        
        if (fadeTime == 0) {
           fadeAlpha = 1;
        }
        else {
           fadeAlpha = 1-(1/FADE_DURATION)*fadeTime;
           
           if (fadeAlpha < 0) fadeAlpha = 0;
           if (fadeAlpha > 1) fadeAlpha = 1;
        }
        
        if (fadeTime >= FADE_DURATION) {
           
           fadeTime = FADE_DURATION;
           enteringScreen = false;
        }
        
        batcher.begin();
        batcher.enableBlending();
        AssetLoader.transitionSprite.setBounds(guiCam.position.x-(WORLD_WIDTH / 2), guiCam.position.y-(WORLD_HEIGHT / 2),
        		WORLD_WIDTH, WORLD_HEIGHT);
        AssetLoader.transitionSprite.draw(batcher, fadeAlpha);
        batcher.end();
     }
	
	
	
	if (leavingScreen) {
        fadeTime += deltaTime;
        
        if (fadeTime == 0) {
           fadeAlpha = 0;
        }
        else {
           fadeAlpha = (1/FADE_DURATION)*fadeTime;
           
           if (fadeAlpha < 0) fadeAlpha = 0;
           if (fadeAlpha > 1) fadeAlpha = 1;
        }
        
        if (fadeTime >= FADE_DURATION) {
           
           fadeTime = FADE_DURATION;
           game.setScreen(nextScreen);
           leavingScreen = false;
        }
        
        batcher.begin();
        batcher.enableBlending();
        AssetLoader.transitionSprite.setBounds(guiCam.position.x-(WORLD_WIDTH / 2), guiCam.position.y-(WORLD_HEIGHT / 2),
        		WORLD_WIDTH, WORLD_HEIGHT);
        AssetLoader.transitionSprite.draw(batcher, fadeAlpha);
        batcher.end();
     }
}
	
	public boolean acceptInput(){
		return(!(enteringScreen || leavingScreen));
	}
	
}